package com.hisense.ovcloud.asop.dao;

import com.hisense.ovcloud.asop.config.Constants;
import com.hisense.ovcloud.asop.dto.req.PublicInfoReq;
import com.hisense.ovcloud.asop.entity.db.WifiUpgradeResult;
import com.hisense.ovcloud.asop.mapper.WifiUpgradeResultMapper;
import com.mybatisflex.annotation.UseDataSource;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.hisense.ovcloud.asop.entity.db.def.WifiUpgradeResultTableDef.WIFI_UPGRADE_RESULT;

@Service
@Slf4j
public class WifiUpgradeResultDao {

    private final CustomerDeviceDao customerDeviceDao;
    final
    WifiUpgradeResultMapper wifiUpgradeResultMapper;

    public WifiUpgradeResultDao(WifiUpgradeResultMapper wifiUpgradeResultMapper, CustomerDeviceDao customerDeviceDao) {
        this.wifiUpgradeResultMapper = wifiUpgradeResultMapper;
        this.customerDeviceDao = customerDeviceDao;
    }

    private QueryWrapper createQueryWrapperForWifiUpgradeResults(List<String> wifiIdList){
        return new QueryWrapper()
                .select(WIFI_UPGRADE_RESULT.ALL_COLUMNS)
                .from(WIFI_UPGRADE_RESULT)
                .orderBy(WIFI_UPGRADE_RESULT.UPDATEDATE, false);
    }


    @UseDataSource(Constants.DS.WGDB)
    public List<WifiUpgradeResult> fetchWifiUpgradeResults(List<String> wifiIdList, PublicInfoReq r){
        if(wifiIdList.isEmpty() && StringUtils.isEmpty(r.getWifiId()) && StringUtils.isEmpty(r.getPuid()) && StringUtils.isEmpty(r.getDeviceId())){
            return new ArrayList<>();
        }else{
            QueryWrapper q = createQueryWrapperForWifiUpgradeResults(wifiIdList);
            if(!wifiIdList.isEmpty()){
                q = q.and(WIFI_UPGRADE_RESULT.WIFIID.in(wifiIdList));
            }
            if(StringUtils.isNotEmpty(r.getWifiId()) || StringUtils.isNotEmpty(r.getPuid()) || StringUtils.isNotEmpty(r.getDeviceId())){
                q = q.and(WIFI_UPGRADE_RESULT.WIFIID.eq(customerDeviceDao.findWifiIdByPuidOrDeviceId(r)));
            }

            return wifiUpgradeResultMapper.selectListByQueryAs(
                    q.limit((r.getPageNum()-1) * r.getPageSize(), r.getPageSize()),
                    WifiUpgradeResult.class);
        }
    }

    @UseDataSource(Constants.DS.WGDB)
    public long fetchWifiUpgradeResultsTotal(List<String> wifiIdList, PublicInfoReq r){
        if(wifiIdList.isEmpty() && StringUtils.isEmpty(r.getWifiId()) && StringUtils.isEmpty(r.getPuid()) && StringUtils.isEmpty(r.getDeviceId())){
            return 0L;
        }else{
            QueryWrapper q = createQueryWrapperForWifiUpgradeResults(wifiIdList);
            if(!wifiIdList.isEmpty()){
                q = q.and(WIFI_UPGRADE_RESULT.WIFIID.in(wifiIdList));
            }
            if(StringUtils.isNotEmpty(r.getWifiId()) || StringUtils.isNotEmpty(r.getPuid()) || StringUtils.isNotEmpty(r.getDeviceId())){
                q = q.and(WIFI_UPGRADE_RESULT.WIFIID.eq(customerDeviceDao.findWifiIdByPuidOrDeviceId(r)));
            }

            return wifiUpgradeResultMapper.selectCountByQuery(
                    q
            );
        }
    }
}
